import { getList,getInvoiceInfoByTransactionFlow } from '@/api/transactionFlowInvoice.js'
import Vue from 'vue'

const flowInvoice = {
    namespaced: true,
    state: {
        flowForm: {
                "issqyw": "", // 业务状态
                "xzjc": "", // 险种简称
                zjlx: '', // 证件类型
                startTime:'', //  交易日期1
                endTime:'', // 交易日期2
                kfhm:"", // 获票人
                "tbdh": "210210046669482", // 投保单号
                "ywbh": "", // 保单号/保全号/定期结算号
                "xpxslh": "", // 学平险受理号
                "sjly": "", // 来源系统
                "isgryw": "", // 是否个人业务
        },
        flowList:[],
        pages:{
            current:1,
            size:10, 
            total: 0
        },
        selectedItems: [],
        invoiceInfo:{

        },
        tableRef:null
    },
    mutations: {
        tableRefVuex(state, val) {
            state.tableRef = val;
        },
        ChangeFLowForm(state, val) { // 改时间
            state.flowForm = val;
        },
        ChangeFlowList(state, val) {
            state.flowList = val.records;
            state.pages = {
                current:val.current,
                size:val.size,
                total:val.total
            };
        },
        handlerPages(state,val){
            state.pages = {
                ...state.pages,
                ...val
            }
        },
        handleSelectionChange(state,val){
            state.selectedItems = val.ids;
        },
        reset(state){
            console.log(1)
            state.flowForm = {
                "issqyw": "", // 业务状态
                "xzjc": "", // 险种简称
                zjlx: '', // 证件类型
                startTime:'', //  交易日期1
                endTime:'', // 交易日期2
                kfhm:'', // 获票人
                "tbdh": "", // 投保单号
                "ywbh": "", // 保单号/保全号/定期结算号
                "xpxslh": "", // 学平险受理号
                "sjly": "", // 来源系统
                "isgryw": "", // 是否个人业务
            }
            state.pages = {
                current:1,
                size:10, 
                total: 0
            }
            state.flowList = []
            state.selectedItems = []
            state.invoiceInfo =   {}
            if(state.tableRef)state.tableRef.clearSelection();
            state.tableRef =   null

        },
        changeInvoiceInfo(state,payload){
            state.invoiceInfo = payload;
        }
    },
    actions: {
        // 交易流水开票查询
        async getList({ commit, state }, payload) {
            let {kfhm,ywbh,tbdh,startTime,endTime } = state.flowForm
            // if(kfhm || ywbh || tbdh || (startTime && endTime)){
                let { data,code ,message } = await getList({
                    ...state.pages,
                    pageNo:state.pages.current,
                    pageSize:state.pages.size,
                    ...state.flowForm
                })
    
                if (code == 200) {
                    if(state.tableRef&&state.tableRef.clearSelection){
                        try {
                            state.tableRef.clearSelection()
                        } catch (error) {
                            
                        }
                    };
                    commit('ChangeFlowList', data)
                }
            // }else{
            //     Vue.prototype['$message'].warning('请选择交易日期、获票人名称、投保单号、保单号/保全号/定期结算号至少一项后在发起查询')
            // }
        },
        // 根据交易流水id查询发票
        getInvoiceByTransactionId({ commit, state }, payload) {
            return new Promise(async (resolve,reject)=>{
                let { data,code ,message } = await getInvoiceInfoByTransactionFlow(state.selectedItems.map(item=>item.id))
    
                if (code == 200) {
                    commit('changeInvoiceInfo', data);
                    resolve(data)
                }else{
                    resolve(false)
                }
            })
        },
    },
}

export default flowInvoice
